//
//  ProvinceListViewController.m
//  AppointmentForCar
//
//  Created by Herron on 2017/1/9.
//  Copyright © 2017年 Herron. All rights reserved.
//

#import "ProvinceListViewController.h"
#import "ProvinceModel.h"
#import "CityListViewController.h"

@interface ProvinceListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *tableArray;

@end

@implementation ProvinceListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"服务地区";
    
    self.tableArray = @[].mutableCopy;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ProvinceCellID"];
//    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    [HUDManager showLoadingHUDViewWithText:kMessage_NetLoading];
    [NetWork postNetWorkWithUrl:kURLProvinceList parame:nil successBlock:^(NSDictionary *dic) {
        NSLog(@"dic = %@", dic);
        [HUDManager hideHUDView];
        self.tableArray = [ProvinceModel arrayOfModelsFromDictionaries:dic[@"data"][@"provinceList"] error:nil];
        [self.tableView reloadData];
    } errorBlock:^(id error) {
        [HUDManager showWarningWithText:kMessage_NetError];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - <UITableViewDataSource, UITableViewDelegate>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ProvinceModel *model = self.tableArray[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ProvinceCellID"];
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = model.name;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ProvinceModel *model = self.tableArray[indexPath.row];
    CityListViewController *vc = [[CityListViewController alloc] init];
    vc.provinceId = model.pId;
    vc.provinceName = model.name;
    vc.type = self.type;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
